package com.yx.yinshanjinniu.adapters;

import android.content.Context;
import android.widget.TextView;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.base.BaseRVAdapter;
import com.yx.yinshanjinniu.base.BaseRVHolder;

import java.util.List;

public class HotTagAdapter extends BaseRVAdapter<String> {
    private OnTagClickListener onTagClickListener;
    private int selectedPosition = 0;

    public HotTagAdapter(Context context, List<String> list, OnTagClickListener onTagClickListener) {
        super(context, list);
        this.onTagClickListener = onTagClickListener;
    }

    @Override
    public int onCreateViewLayoutID(int viewType) {
        return R.layout.item_hot_tag;
    }

    @Override
    public void onBindView(BaseRVHolder holder, int position) {
        String tag = mList.get(position);
        TextView tv_hot_tag = holder.getViewHolder().get(R.id.tv_hot_tag);
        tv_hot_tag.setText(tag);

        // 设置选中状态
        tv_hot_tag.setSelected(position == selectedPosition);
        tv_hot_tag.setOnClickListener(v -> {
            if (selectedPosition == position) return;
            int oldPosition = selectedPosition;
            selectedPosition = position;
            notifyItemChanged(oldPosition);
            notifyItemChanged(selectedPosition);
            onTagClickListener.onTagClick(tag);
        });
    }

    public interface OnTagClickListener {
        void onTagClick(String tag);
    }

}
